package com.xinrui.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xinrui.sunnyday.constant.MessageConstant;
import com.xinrui.sunnyday.entity.Result;
import com.xinrui.sunnyday.pojo.ReportSetMeal;
import com.xinrui.sunnyday.service.ReportService;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.FormParam;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;

@ResponseBody
@Controller
@RequestMapping("/report")
public class ReportController {
    @Reference
    private ReportService reportService;

    @GetMapping("/getMemberReport")
    public Result getMemberReport() {
//        1.拿到当前年份的前一年
//        拿到当前年份根据当前年份减一就得到了去年的年份
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);
//        过去的一年的月每个月
        ArrayList<String> months = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            //        拿到日期进行格式转换
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(calendar.getTime());
            months.add(format);
        }
//        2.将日期传到service层,返回集合
        Map<String, Object> map = reportService.getMemberReport(months);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
    }

    @GetMapping("/getSetMealReport")
    public Result getSetMealReport() {
//        1.直接查询数据库,返回一个集合对象(我这里直接封装成了一个对象)
        List<ReportSetMeal> setMealCount = reportService.getSetMealReport();
//        2.通过查询数据库的集合对象拿到套餐名称封装成一个集合
        ArrayList<String> setMealNames = new ArrayList<>();
        for (ReportSetMeal reportSetMeal : setMealCount) {
//            拿到套餐名称
            String name = reportSetMeal.getName();
            setMealNames.add(name);
        }
//        再将套餐名称和集合对象封装到map中
        Map<String, Object> map = new HashMap<>();
        map.put("setMealCount", setMealCount);
        map.put("setMealNames", setMealNames);
        return new Result(true, MessageConstant.GET_BUSINESS_REPORT_FAIL, map);
    }

    /**
     * 获取运营数据
     *
     * @return
     */
    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        Map<String, Object> reportData = reportService.getBusinessReportData();
        return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, reportData);
    }

    @GetMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpServletRequest request, HttpServletResponse response) {
//        拿到运营数据
        Map<String, Object> reportData = reportService.getBusinessReportData();
//       获取模板路径
        String realPath = request.getSession().getServletContext().getRealPath("/template/report_template.xlsx");
        try {
            //       创建工作薄
            FileInputStream fileInputStream = new FileInputStream(realPath);
            ServletOutputStream outputStream = response.getOutputStream();
//           在内存中创建工作薄
            XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
//           获取工作表
            XSSFSheet sheetAt = sheets.getSheetAt(0);
//           日期
            sheetAt.getRow(2).getCell(5).setCellValue((String)reportData.get("reportDate"));
//           会员统计
//           今日新增会员
            sheetAt.getRow(4).getCell(5).setCellValue((Integer) reportData.get("todayNewMember"));
//           本周新增会员
            sheetAt.getRow(5).getCell(5).setCellValue((Integer) reportData.get("thisWeekNewMember"));
//           总会员人数
            sheetAt.getRow(4).getCell(7).setCellValue((Integer) reportData.get("totalMember"));
//           本月新增会员
            sheetAt.getRow(5).getCell(7).setCellValue((Integer) reportData.get("thisMonthNewMember"));
//           预约统计
//            今日已预约人数
            sheetAt.getRow(7).getCell(5).setCellValue((Integer) reportData.get("todayOrderNumber"));
//            本周已预约人数
            sheetAt.getRow(8).getCell(5).setCellValue((Integer) reportData.get("thisWeekOrderNumber"));
//            本月已预约人数
            sheetAt.getRow(9).getCell(5).setCellValue((Integer) reportData.get("thisMonthOrderNumber"));
//            今日已到诊人数
            sheetAt.getRow(7).getCell(7).setCellValue((Integer) reportData.get("todayVisitsNumber"));
//            本周已到诊人数
            sheetAt.getRow(8).getCell(7).setCellValue((Integer) reportData.get("thisWeekVisitsNumber"));
//            本月已到诊人数
            sheetAt.getRow(9).getCell(7).setCellValue((Integer) reportData.get("thisMonthVisitsNumber"));
//            套餐热门
            List<Map<String, Object>> hotSetMeal = (List<Map<String, Object>>) reportData.get("hotSetMeal");
//                将行数定死
            int row = 12;
//            循环map
            for (Map<String, Object> map : hotSetMeal) {
                sheetAt.getRow(row).getCell(4).setCellValue(String.valueOf(map.get("name")));
                sheetAt.getRow(row).getCell(5).setCellValue(String.valueOf(map.get("setmeal_count")));
                sheetAt.getRow(row).getCell(6).setCellValue(String.valueOf(map.get("proportion")));
                sheetAt.getRow(row).getCell(7).setCellValue(String.valueOf(map.get("remark")));
                row++;
            }
            String s = "运营统计数据报表.xlsx";
//            解决乱码问题
            s = new String(s.getBytes(), "ISO-8859-1");
//           设置头信息
            response.setHeader("Content-Disposition", "attachement;filename=" + s);
//            设置内容体的文件格式是excel
            response.setContentType("application/vnd.ms-excel");
            sheets.write(outputStream);
            outputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
